package cn.e2win.biz.zdzx;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.e2win.base.controller.MyException;
import cn.e2win.base.entity.SysAttach;
import cn.e2win.base.entity.SysUser;
import cn.e2win.base.json.vo.Pager;
import cn.e2win.biz.base.BaseDAO;
import cn.e2win.biz.base.BaseService;
import cn.e2win.biz.base.CommonService;
import cn.e2win.biz.dbo.ZdzxEmp;
import cn.e2win.biz.user.UserDao;


@Service
public class ZdzxService extends BaseService{
	

	@Autowired
	private ZdzxDao zdzxDao;
	@Autowired
	private UserDao userDao;
	@Autowired
	private CommonService commonService;
	
	@Override
	public BaseDAO initDao() {
		return zdzxDao;
	}


	@Transactional
	public ZdzxEmp editZdzxEmp(ZdzxEmp obj) throws MyException {
		//新添加用户
		if(obj.getId()==null){
			if(commonService.getEmpByMobile(obj.getMobile()).size()!=0){
				throw new MyException("当前用户的手机号【" + obj.getMobile() + "】已注册，请检查后重试！");
			}
		}else{
			obj.setMobile(null);
		}
		
		//Logo
		if(obj.getIcon().getSize()>0){
			SysAttach attach = uploadFile(obj.getIcon());
			obj.setIconUrl(attach.getFilePath());
		}
		
		ZdzxEmp emp = (ZdzxEmp)super.edit(obj);//保存用户
		if(obj.getId()==null){
			commonService.saveUser4Emp(emp);
		}else{
			SysUser user =userDao.getUserByType(obj.getClass().getSimpleName(), obj.getId());
			user.setUserName(emp.getEmpName());
			user.setIsLeader(emp.getIsLeader());
			user.setTelNo(emp.getMobile());
		}
		return emp;
	}

	public void pagerZdzxEmp(Pager pager) {
		zdzxDao.pagerZdzxEmp(pager);
	}



}

